package it.infodea.tapestrydea.services.mail.impl;

import it.infodea.tapestrydea.services.mail.AttachedFilesManager;

import java.io.File;
import java.io.IOException;

import org.apache.tapestry5.upload.services.UploadedFile;

public class AttachedFilesManagerImpl implements AttachedFilesManager {

	private static final String basePath = System.getProperty("java.io.tmpdir");
	
	public String findTempAttachedFile(String name) {
		String pathname = basePath + name;
		File file = new File(pathname);
		return file.getAbsolutePath();
	}

	public File loadTempAttachedFile(String name) {
		String pathname = basePath + name;
		File file = new File(pathname);
		return file;
	}

	public String storeTempAttachedFile(String name, UploadedFile upFile) {
		String pathname = basePath + name;
		File file = new File(pathname);
		if(!file.exists())
			try {
				file.createNewFile();
				upFile.write(file);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		return file.getAbsolutePath();
	}

}
